package com.tongzhou.tongzhou.quot.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.initInfo.model.Region;
import com.tongzhou.tongzhou.initInfo.model.SearchRegionCondition;
import com.tongzhou.tongzhou.quot.dao.IPaperDAO;
import com.tongzhou.tongzhou.quot.dao.IPaperTypeDAO;
import com.tongzhou.tongzhou.quot.dao.IRegionDAO;
import com.tongzhou.tongzhou.quot.model.Paper;
import com.tongzhou.tongzhou.quot.model.PaperType;
import com.tongzhou.util.FileUtil;
import com.tongzhou.util.Util;

public class QuotRegionAction extends ActionSupport{
	private int id;
	private Region region;
	private List<Region> result=null;
	private SearchRegionCondition condition=null;
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private String keng;
	
	private IRegionDAO dao=null;
	private IPaperTypeDAO paperTypeDAO=null;
	private IPaperDAO paperDAO=null;
	private List<Region> regions;
	
	private File quoturl ;
	private String quoturlFileName ;
	private String region_id;//区域id
	private List<PaperType> paperTypes;
	List<Map<String, List<Paper>>> list = new ArrayList<Map<String,List<Paper>>>() ;
	/**
	 * 纸质列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listRegion() throws DBException{
		checkSearchCondition();
		condition = new SearchRegionCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
	    result = dao.listRegion(condition);
		return "listRegion";
	} 
	
	public String listRegionTemp() throws DBException{
		paperTypes=dao.getPaperTypeList(region_id);
		for(PaperType pt:paperTypes){
			List<Paper> papers=dao.getPaperList(pt.getId());
			Map<String,List<Paper>> map = new HashMap<String, List<Paper>>();
			map.put(pt.getPaper_type_name(),papers);
			list.add(map);
		}
		return "listRegionTemp";
	}
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
	
	/**
	 * 新增纸质
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toAddRegion() throws DBException{
		regions=dao.getRegionList();
		return "addRegion";
	}
	
	public String addRegion() throws DBException{
		if(quoturl!=null){
			String s=quoturlFileName.replace(".", "`").split("`")[1];
			String path = "quot"+File.separator+FileUtil.generateFileName()+"."+s;
			path=path.replace("\\","\\\\");
			FileUtil.copyFile(quoturl, new File(FileUtil.getWebRootDir()+path));
			region.setUrl(path);
		}
		if(region.getId()!=0){
			dao.updateRegion(region);
		}else{
			dao.addRegion(region);
		}
		uploadClxxFile();
		listRegion();
		return "listRegion";
	}
	
	public String deleteRegion() throws DBException{
//		dao=new UserMgrDAO();
		dao.deleteRegion(id);
		listRegion();
		return "listRegion";
	}
	
	public String toEditRegion() throws DBException{
		region=dao.getRegionById(id);
		regions=dao.getRegionList();
		return "editRegion";
	}
	
	public String editRegion() throws DBException{
		if(quoturl!=null){
			String s=quoturlFileName.replace(".", "`").split("`")[1];
			String path = "quot"+File.separator+FileUtil.generateFileName()+"."+s;
			path=path.replace("\\","\\\\");
			FileUtil.copyFile(quoturl, new File(FileUtil.getWebRootDir()+path));
			region.setUrl(path);
		}else{
			region.setUrl(region.getUrl().replace("\\","\\\\"));
		}
		dao.updateRegion(region);
		uploadClxxFile();
		listRegion();
		return "listRegion";
	}
	
	
	/**
	 * 解析excel
	 */
	public void uploadClxxFile(){
		try {
			if(quoturl!=null){
				HSSFSheet sheet=FileUtil.readExcel(quoturl);
				uploadClxxApp(sheet);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ;
		}
	}
	
	public void uploadClxxApp(HSSFSheet sheet) throws Exception {
		int FirstRowNum = sheet.getFirstRowNum();
		int LastRowNum = sheet.getLastRowNum();
		try {
			int j=0;
			int k=0;
			TreeMap<Integer,String> map=new TreeMap<Integer,String>();
			HSSFRow row2 = sheet.getRow(FirstRowNum);
			if (row2 != null) {
				HSSFCell cell = row2.getCell(j);
				while(cell!=null){
					PaperType paperType=new PaperType();
					String paper_type_name = FileUtil.getHSSFCellValue(cell);
					if(paper_type_name.equals("")){
						k++;
						cell = row2.getCell(k);
						continue;
					}
					paperType.setPaper_type_name(paper_type_name);
					paperType.setRegion_id(region.getId());
					String newId=Util.getNewID();
					paperType.setId(newId);
					paperTypeDAO.addPaperType(paperType);
					map.put(j, newId);
					j++;
					k++;
					cell = row2.getCell(k);
				}
			}
			for (int i = FirstRowNum+2; i <= LastRowNum; i++) {
				HSSFRow row = sheet.getRow(i);
				if (row != null) {
					for(int m=0;m<j;m++){
						Paper paper=new Paper();
						HSSFCell cell1 = row.getCell(m*2);
						if(cell1==null){
							continue;
						}
						
						String paper_name = FileUtil.getHSSFCellValue(cell1);
						if(paper_name==null||paper_name.equals("")){
							continue;
						}
						paper.setPaper_name(paper_name);
						
						HSSFCell cell2 = row.getCell(m*2+1);
						String price = FileUtil.getHSSFCellValue(cell2);
						paper.setPrice(price);
						
						paper.setKeng("1");
						paper.setPaper_type_id(map.get(m));
						
						paperDAO.addPaper(paper);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("插入数据时发生异常");
		}
	}

	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public Region getRegion() {
		return region;
	}


	public void setRegion(Region region) {
		this.region = region;
	}


	public List<Region> getResult() {
		return result;
	}


	public void setResult(List<Region> result) {
		this.result = result;
	}


	public SearchRegionCondition getCondition() {
		return condition;
	}


	public void setCondition(SearchRegionCondition condition) {
		this.condition = condition;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public String getOrderBy() {
		return orderBy;
	}


	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}


	public String getOrderByType() {
		return orderByType;
	}


	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}


	public String getKeng() {
		return keng;
	}


	public void setKeng(String keng) {
		this.keng = keng;
	}


	public IRegionDAO getDao() {
		return dao;
	}


	public void setDao(IRegionDAO dao) {
		this.dao = dao;
	}


	public List<Region> getRegions() {
		return regions;
	}


	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}


	public File getQuoturl() {
		return quoturl;
	}


	public void setQuoturl(File quoturl) {
		this.quoturl = quoturl;
	}


	public String getQuoturlFileName() {
		return quoturlFileName;
	}


	public void setQuoturlFileName(String quoturlFileName) {
		this.quoturlFileName = quoturlFileName;
	}


	public IPaperTypeDAO getPaperTypeDAO() {
		return paperTypeDAO;
	}


	public void setPaperTypeDAO(IPaperTypeDAO paperTypeDAO) {
		this.paperTypeDAO = paperTypeDAO;
	}


	public IPaperDAO getPaperDAO() {
		return paperDAO;
	}


	public void setPaperDAO(IPaperDAO paperDAO) {
		this.paperDAO = paperDAO;
	}


	public String getRegion_id() {
		return region_id;
	}

	public void setRegion_id(String regionId) {
		region_id = regionId;
	}

	public List<PaperType> getPaperTypes() {
		return paperTypes;
	}

	public void setPaperTypes(List<PaperType> paperTypes) {
		this.paperTypes = paperTypes;
	}

	public List<Map<String, List<Paper>>> getList() {
		return list;
	}

	public void setList(List<Map<String, List<Paper>>> list) {
		this.list = list;
	}
	
}
